<?php
//微信红包类
class WXHB{

    private $xml_str="
<xml>
<sign><![CDATA[%s]]></sign>
<mch_billno><![CDATA[%s]]></mch_billno>
<mch_id><![CDATA[%s]]></mch_id>
<wxappid><![CDATA[%s]]></wxappid>
<send_name><![CDATA[%s]]></send_name>
<re_openid><![CDATA[%s]]></re_openid>
<total_amount><![CDATA[%s]]></total_amount>
<total_num><![CDATA[%s]]></total_num>
<wishing><![CDATA[%s]]></wishing>
<client_ip><![CDATA[%s]]></client_ip>
<act_name><![CDATA[%s]]></act_name>
<remark><![CDATA[%s]]></remark>
<nonce_str><![CDATA[%s]]></nonce_str>
</xml>
";

        //private $nonce_str='5K8264ILTKCH16CQ2502SI8ZNMTM67VS';      //随机字符串
//    private $sign='';           //签名
//    private $mch_billno='';     //商户订单号
        private $mch_id='1411306102';         //商户号
        private $wxappid='wxdfc6fec1902c9870';        //公众账号appid
        private $send_name='晋商农夫果园';      //商户名称
//    private $re_openid='';      //用户openid
       // private $total_amount=1;   //付款金额
        private $total_num=1;      //红包发放总人数
        private $wishing='恭喜发财';        //红包祝福语
//    private $client_ip='';      //Ip地址
        private $act_name='晋商农夫果园';       //活动名称
        private $remark='奖金';         //备注


    public function send($user_post,$money){
        //网址
        $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
        $total_amount=500;   //付款金额
      
        $nonce_str=$this->suiji_str();
//        $nonce_str='aaaaaaaaaaaaaaa';
        $mch_billno=$this->dingdanhao($this->mch_id);     //商户订单号
//        $mch_billno='1367908902201608061234567890';     //商户订单号
        $host = GetHostByName($_SERVER['SERVER_NAME']);
        $sign=$this->get_sign($nonce_str,$mch_billno,$user_post,$money,$host);

        $post_data=sprintf($this->xml_str,$sign,$mch_billno,$this->mch_id,$this->wxappid,$this->send_name,$user_post,$total_amount,$this->total_num,$this->wishing,$host,$this->act_name,$this->remark,$nonce_str);
        
       
        $data=$this->curl_post_ssl($url,$post_data);
        return $data;
    }

    public function get_sign($nonce_str,$mch_billno,$user_post,$money,$host){
        $appkey='2f6643d8d42a16646da60953e61f2af3';
        $post_data = array (
            'mch_billno' => $mch_billno,
            'mch_id'     => $this->mch_id,
            'wxappid' => $this->wxappid,
            'send_name' => $this->send_name,
            're_openid' => $user_post,
            'total_amount' => $money,
            'total_num' => $this->total_num,
            'wishing' => $this->wishing,
            'client_ip' =>$host,
            'act_name' => $this->act_name,
            'remark' => $this->remark,
            'nonce_str' => $nonce_str,
            );
        ksort($post_data);
        $str='';
        foreach($post_data as $key=>$value){
            $str.=$key.'='.$value.'&';
        }
        $str=trim($str,'&');
        $str.='&key='.$appkey;
//        echo '连接key后：';
//        var_dump($str);
        $md5=md5($str);
        $result=strtoupper($md5);
//        var_dump($post_data);
//        var_dump($result);
        return $result;
    }

    public function dingdanhao($mch_id){
        $str=(string)$mch_id;//商户id
        $str.=date('Ymd');//yyyymmdd
        $range=range('0','9');
        for($i=0;$i<10;$i++){
            $str.=array_rand($range,1);
        }
        return $str;
    }
    public function suiji_str($int=30){
        $zimu=range('A','Z');
        $shuzi=range('1','9');
        $tmp=array_merge($zimu,$shuzi);
        $result='';
        for( $i=0;$i<$int;$i++) {
            $result.=$tmp[array_rand($tmp,1)];
        }
        return $result;
    }

    function curl_post_ssl($url, $vars, $second=30,$aHeader=array())
    {

        var_dump($vars);
        $ch = curl_init();

        //超时时间
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        //这里设置代理，如果有的话
        //curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);

        //以下两种方式需选择一种

        //第一种方法，cert 与 key 分别属于两个.pem文件
        //默认格式为PEM，可以注释
        //curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLCERT,dirname(__FILE__).'\ssl\apiclient_cert.pem');
        //默认格式为PEM，可以注释
        //curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__).'\ssl\apiclient_key.pem');

        curl_setopt($ch,CURLOPT_CAINFO,dirname(__FILE__).'\ssl\rootca.pem');
//        //文件名
//        echo '文件名现在：';
//        echo dirname(__FILE__).'\ssl\apiclient_key.pem';
//
//        //文件是否存在
//        echo '文件是否存在现在：';
//        var_dump(file_exists(dirname(__FILE__).'\ssl\apiclient_key.pem'));
//
//
//        echo '原来文件名：';
//        echo __DIR__.'/ssl/apiclient_key.pem';
//        //文件是否存在
//        echo '文件是否存在原来：';
//        var_dump(file_exists(__DIR__.'\ssl\apiclient_key.pem'));



        //第二种方式，两个文件合成一个.pem文件
//        curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');


        if( count($aHeader) >= 1 ){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        }

        // post数据
        curl_setopt($ch,CURLOPT_POST, 1);
        // post的变量
        curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
        $data = curl_exec($ch);
        $data2=curl_multi_getcontent($ch);
        if($data){
            curl_close($ch);
              print_r($data2);
              echo"</br>";
               print_r($data);
              echo"</br>";

            // print_r($data);
            return $data2;
        }else {
            $error = curl_error($ch);
            echo "call faild, errorCode:$error\n";
            curl_close($ch);
            return false;
        }
    }
}

?>

